package com.diyunapp.xilinapp.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.BuyCarGoodsOutBean;
import com.diyunapp.xilinapp.bean.MyOrdersOutBean;
import com.diyunapp.xilinapp.bean.OrderPayOutBean;
import com.diyunapp.xilinapp.myapi.BuyCarGoodsInfoChageInterface;
import com.diyunapp.xilinapp.myapi.MyOrdersClickInterface;
import com.diyunapp.xilinapp.myapi.OnRecyclerViewItemClickListener;
import com.diyunapp.xilinapp.utils.FormatConfirm;
import com.diyunapp.xilinapp.utils.ScrollListViewDisplay;

import java.util.List;

/**
 * Created by NIDAYE on 2016/6/15.
 */
public class RecycleViewMyordersAdapter extends RecyclerView.Adapter<RecycleViewMyordersAdapter.MyViewHolder> implements View.OnClickListener{
    private static final int TYPE_HEADER = 0, TYPE_ITEM = 1, TYPE_FOOT = 2;

    public List<MyOrdersOutBean> mDatas;
    public  Context myContext;
    private View headView;
    private View footView;
    private int headViewSize = 0;
    private int footViewSize = 0;
    private ChangeGridLayoutManagerSpance changeGridLayoutManager;
    private boolean isAddFoot=false;
    private boolean isAddHead=false;

    private MyOrdersClickInterface clickInterface;

    //定义实现onItemClickClickListener 的接口
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public interface ChangeGridLayoutManagerSpance{
        public void change(int size, boolean isAddHead, boolean isAddFoot);
    }
    //提供接口给 让LayoutManager根据添加尾部 头部与否来做判断 显示头部与底部的SpanSize要在添加头部和尾部之后
    public void setChangeGridLayoutManager(ChangeGridLayoutManagerSpance changeGridLayoutManager){
        this.changeGridLayoutManager=changeGridLayoutManager;
        changeGridLayoutManager.change(getItemCount()-1,isAddHead,isAddFoot);
    }

    public RecycleViewMyordersAdapter(List<MyOrdersOutBean> mDatas, Context myContext, MyOrdersClickInterface clickInterface) {
        this.mDatas = mDatas;
        this.myContext = myContext;
        this.clickInterface = clickInterface;
    }

    public void addHeadView(View view) {
        headView = view;
        headViewSize = 1;
        isAddHead=true;
    }

    public void addFootView(View view) {
        footView = view;
        footViewSize = 1;
        isAddFoot=true;
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;

        if (headViewSize==1 && position == 0) {
            type = TYPE_HEADER;
        } else if (footViewSize==1 && position == getItemCount()-1) {
            //最后一个位置
            type = TYPE_FOOT;
        }
        return type;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        Log.i("zhangxiaoxidebug","MyViewHolder onCreateViewHolder");
        View view = null;
        switch (i) {
            case TYPE_HEADER:
                view = headView;
                break;

            case TYPE_ITEM:
                view = View.inflate(viewGroup.getContext(), R.layout.myorders_out_list, null);
                view.setOnClickListener(this);
                break;

            case TYPE_FOOT:
                view =footView;
                break;
        }
        return new MyViewHolder(view,i);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        if(getItemViewType(position)==TYPE_ITEM)
        {
            int dataPosition = position - headViewSize;
            Log.i("zhangxiaoxidebug","myOrders dataPosition="+dataPosition);
            final MyOrdersOutBean currentdata = mDatas.get(dataPosition);
            final int state = FormatConfirm.stringToInt(currentdata.getPay_state());
            float money = FormatConfirm.stringToFloat(currentdata.getPay_money());
            switch (state)
            {
                case 0:
                    holder.paysn_money_tv.setText("订单已取消");
                    holder.paysn_pay_tv.setVisibility(View.GONE);
                    break;
                case 10:
                    holder.paysn_money_tv.setText("在线支付：￥"+currentdata.getPay_money());
                    holder.paysn_pay_tv.setText("立即付款");
                    holder.paysn_pay_tv.setVisibility(View.VISIBLE);
                    break;
                case 20:
                    holder.paysn_money_tv.setText("等待卖家发货");
                    holder.paysn_pay_tv.setVisibility(View.GONE);
                    break;
                case 30:
                    holder.paysn_money_tv.setText("卖家已发货");
                    holder.paysn_pay_tv.setVisibility(View.GONE);
                    break;
                case 40:
                    holder.paysn_money_tv.setText("订单已完成");
                    holder.paysn_pay_tv.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }

            holder.paysn_pay_tv.setTag(position);
            holder.paysn_pay_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(state == 10)
                    {
                        clickInterface.OnOrdersClick(v,state,currentdata.getPay_sn());
                    }
                }
            });


            //订单列表
            final OrderPayOuterAdapter myOrdersAdapter = new OrderPayOuterAdapter(currentdata.getOrdersList(), myContext, new MyOrdersClickInterface() {
                @Override
                public void OnOrdersClick(View view, int operation, String info) {
                    clickInterface.OnOrdersClick(view,operation,info);
                }
            });
            holder.orders_lv.setAdapter(myOrdersAdapter);
            myOrdersAdapter.notifyDataSetChanged();
            ScrollListViewDisplay.setListViewHeightBasedOnChildren(holder.orders_lv);
            //将数据保存在itemView的Tag中，以便点击时进行获取
            holder.itemView.setTag(dataPosition);
        }
    }

    @Override
    public int getItemCount()
    {
        return mDatas.size() + headViewSize + footViewSize;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ListView orders_lv;
        TextView paysn_money_tv;
        TextView paysn_pay_tv;

        public MyViewHolder(View itemView,int type) {
            super(itemView);
            Log.i("zhangxiaoxidebug","myOrders recycle viewholder-----------");
            if(type == TYPE_ITEM )
            {
                orders_lv =(ListView) itemView.findViewById(R.id.myorders_totalout_listview);
                paysn_money_tv =(TextView)itemView.findViewById(R.id.myorders_paysn_money_tv);
                paysn_pay_tv =(TextView)itemView.findViewById(R.id.myorders_paysn_payatonce_tv);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}



